﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BOSS.Framework.Search.UrlBuilder;

namespace BOSS.Framework.Search.Context
{
    /// <summary>
    /// 范围型的过滤条件部分抽象类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class RangeValueFilterPart<T> : IFilterPart
    {
        /// <summary>
        /// 获取 / 设置 最小值
        /// </summary>
        public T MinValue { get; set; }

        /// <summary>
        /// 获取 / 设置 是否包含最小值
        /// </summary>
        public bool IncludeMinValue { get; set; }

        /// <summary>
        /// 获取 / 设置 最大值
        /// </summary>
        public T MaxValue { get; set; }

        /// <summary>
        /// 获取 / 设置 是否包含最大值
        /// </summary>
        public bool IncludeMaxValue { get; set; }

        #region IQueryPart 成员

        /// <summary>
        /// 获取 字段名称
        /// </summary>
        public string FieldName { get; protected set; }

        /// <summary>
        /// 构造 URL
        /// </summary>
        /// <param name="urlBuilder"></param>
        public abstract void BuildUrl(IUrlBuilder urlBuilder);

        #endregion

        #region IFilterPart 成员

        /// <summary>
        /// 获取 过滤条件
        /// </summary>
        public virtual string Filter
        {
            get
            {
                StringBuilder query = new StringBuilder();
                if (this.IncludeMinValue)
                {
                    query.Append('[');
                }
                else
                {
                    query.Append('{');
                }
                query.Append(GetValue(this.MinValue))
                    .Append(" TO ")
                    .Append(GetValue(this.MaxValue));
                if (this.IncludeMaxValue)
                {
                    query.Append(']');
                }
                else
                {
                    query.Append('}');
                }
                return query.ToString();
            }
        }

        #endregion

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected virtual object GetValue(T value)
        {
            return value;
        }
    }
}
